package com.maxatime.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @Author 7月份考试
* @Description
* @Date
* @Return
* @param
*/
public class Ssbcontroller9 {
    public static void main(String[] args) {
        try {
            // 创建输入流
            String filePath = "C:\\Users\\saxas\\Desktop\\用户信息.xlsx";

            int suffix = filePath.lastIndexOf(".");
            //获取文件后缀名
            String substring = filePath.substring(suffix + 1);
            if (!"xlsx".equals(substring) && !"xls".equals(substring)) {
                System.out.printf("不是excel文件");
            }
            //xlsx
            FileInputStream fis = new FileInputStream(filePath);

            // 创建工作簿对象
            Workbook workbook = null;
            if ("xlsx".equals(substring) ) {//xlsx
                workbook = WorkbookFactory.create(fis);
            }else {//xls
                workbook = new XSSFWorkbook(fis);
            }




            // 获取第二个工作表
            Sheet sheet1 = workbook.getSheetAt(1);

            // 获取行数和列数
            int rowCount1 = sheet1.getLastRowNum() + 1;
            int columnCount1 = sheet1.getRow(0).getLastCellNum();
            // 循环遍历每一行
            for (int i = 0; i < rowCount1; i++) {
                Row row = sheet1.getRow(i);

                // 判断是否为空行
                boolean isRowEmpty = true;
                for (int j = 0; j < columnCount1; j++) {
                    //在调用row.getCell(j)方法之前，可以先检查当前行是否为空或是否为null。
                    if (row != null && row.getRowNum() >= 0) {
                        Cell cell = row.getCell(j);
                        if (cell != null && cell.getCellType() != CellType.BLANK) {
                            isRowEmpty = false;
                            break;
                        }
                    }
                }
                if (isRowEmpty) {
                    continue;
                }

                // 遍历每一列
                for (int j = 0; j < columnCount1; j++) {
                    Cell cell = row.getCell(j);

                    // 获取单元格的值
                    String cellValue = "";
                    if (cell != null) {
                        cell.setCellType(CellType.STRING);
                        cellValue = cell.getStringCellValue();
                    }
                    switch (j) {
                        case 0:
                            if (StringUtils.isNotBlank(cellValue)) {
                                System.out.print(cellValue + "\t");
                            }
                            break;
                       /* case 1:
                            if (StringUtils.isNotBlank(cellValue)) {
                                System.out.print(cellValue + "\t");
                            }
                            break;*/
                        case 2:
                            if (StringUtils.isNotBlank(cellValue)) {
                                System.out.print(cellValue + "\t");
                            }
                            break;
                        case 3:
                            if (StringUtils.isNotBlank(cellValue)) {
                                System.out.print(cellValue + "\t");
                            }
                            break;
                    }
                }
                System.out.println();
            }
            // 关闭工作簿和输入流
            workbook.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
